package com.vercon.commonmirror.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;

import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.TextView;

import com.vercon.commonmirror.Constants;
import com.vercon.commonmirror.R;
import com.vercon.commonmirror.utils.DpOrPxUtils;

import androidx.annotation.Nullable;


public class DoubleWaveProgressView extends View {

    private Paint mCirclePaint;//绘制圆环
    //外接圆环颜色
    private int outlineCircleColor;


    private Paint circlePaint;//圆形进度框画笔
    private Paint wavePaint;//绘制波浪画笔
    private Path wavePath;//绘制波浪Path
    private Paint secondWavePaint;//绘制第二个波浪的画笔

    private Bitmap bitmap;//缓存bitmap
    private Canvas bitmapCanvas;

    private WaveProgressAnim waveProgressAnim;
    private TextView textView;
    private OnAnimationListener onAnimationListener;

    private float waveWidth;//波浪宽度
    private float waveHeight;//波浪高度
    private int waveNum;//波浪组的数量（一次起伏为一组）
    private float waveMovingDistance;//波浪平移的距离

    private int viewSize;//重新测量后View实际的宽高
    private int defaultSize;//自定义View默认的宽高

    private float percent;//进度条占比
    private float progressNum;//可以更新的进度条数值
    private float maxNum;//进度条最大值

    private int waveColor;//波浪颜色
    private int secondWaveColor;//第二层波浪颜色
    private int bgColor;//背景进度框颜色

    private boolean isDrawSecondWave;


    public DoubleWaveProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DoubleWaveProgressView);
        waveWidth = typedArray.getDimension(R.styleable.DoubleWaveProgressView_double_wave_width, DpOrPxUtils.dip2px(context, 25));
        waveHeight = typedArray.getDimension(R.styleable.DoubleWaveProgressView_double_wave_height, DpOrPxUtils.dip2px(context, 5));
        waveColor = typedArray.getColor(R.styleable.DoubleWaveProgressView_first_wave_color, Color.GREEN);
        secondWaveColor = typedArray.getColor(R.styleable.DoubleWaveProgressView_second_wave_color, Color.GRAY);
        bgColor = typedArray.getColor(R.styleable.DoubleWaveProgressView_double_wave_bg_color, Color.GRAY);
        outlineCircleColor = typedArray.getColor(R.styleable.DoubleWaveProgressView_widget_outline_circle_color, Color.parseColor("#08FECC"));
        mCircleWidth = typedArray.getDimension(R.styleable.DoubleWaveProgressView_widget_circleWidth_new, Constants.DEFAULT_ARC_WIDTH);
        typedArray.recycle();

        defaultSize = DpOrPxUtils.dip2px(context, context.getResources().getDimension(R.dimen.dp_330));
        waveNum = (int) Math.ceil(Double.parseDouble(String.valueOf(defaultSize / waveWidth / 2)));

        waveMovingDistance = 0;

        wavePath = new Path();

        wavePaint = new Paint();
        wavePaint.setColor(waveColor);
        wavePaint.setAntiAlias(true);//设置抗锯齿
        wavePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        secondWavePaint = new Paint();
        secondWavePaint.setColor(secondWaveColor);
        secondWavePaint.setAntiAlias(true);//设置抗锯齿
        secondWavePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//因为要覆盖在第一层波浪上，且要让半透明生效，所以选此模式

        circlePaint = new Paint();
        circlePaint.setColor(bgColor);
        circlePaint.setAntiAlias(true);//设置抗锯齿



        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStrokeWidth(mCircleWidth);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeCap(Paint.Cap.ROUND);
        mRectF = new RectF();
        mCenterPoint = new Point();

        waveProgressAnim = new WaveProgressAnim();
        waveProgressAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                /*if(percent == progressNum / maxNum){
                    waveProgressAnim.setDuration(5000);
                }*/
            }
        });

        percent = 0;
        progressNum = 0;
        maxNum = 100;
        isDrawSecondWave = true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = measureSize(defaultSize, heightMeasureSpec);
        int width = measureSize(defaultSize, widthMeasureSpec);
        int min = Math.min(width, height);// 获取View最短边的长度
        setMeasuredDimension(min, min);// 强制改View为以最短边为长度的正方形
        viewSize = min;
        waveNum = (int) Math.ceil(Double.parseDouble(String.valueOf(viewSize / waveWidth / 2)));

    }

    private int measureSize(int defaultSize, int measureSpec) {
        int result = defaultSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bitmap = Bitmap.createBitmap(viewSize, viewSize, Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);

        bitmapCanvas.drawCircle(viewSize / 2, viewSize / 2, viewSize / 2, circlePaint);
        bitmapCanvas.drawPath(getWavePath(), wavePaint);
        if (isDrawSecondWave) {
            bitmapCanvas.drawPath(getSecondWavePath(), secondWavePaint);
        }
        canvas.drawBitmap(bitmap, 0, 0, null);
        drawCircle(canvas);
    }

    //圆心
    private Point mCenterPoint;
    private float mRadius;
    //圆环宽度
    private float mCircleWidth = 5;
    //圆的外接矩形
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        //view的宽和高,相对于父布局(用于确定圆心)

        int minSize = Math.min(getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - 2 * (int) mCircleWidth,
                getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - 2 * (int) mCircleWidth);
        mRadius = minSize / 2;
        mCenterPoint.x = getMeasuredWidth() / 2;
        mCenterPoint.y = getMeasuredHeight() / 2;
        //绘制圆弧的边界
        mRectF.left = mCenterPoint.x - mRadius - mCircleWidth / 2 ;
        mRectF.top = mCenterPoint.y - mRadius - mCircleWidth / 2 ;
        mRectF.right = mCenterPoint.x + mRadius + mCircleWidth / 2 ;
        mRectF.bottom = mCenterPoint.y + mRadius + mCircleWidth / 2 ;
//        mCenterPoint.x = getMeasuredWidth() / 2;
//        mCenterPoint.y = getMeasuredHeight() / 2;
//        mRectF.left = (float) 2.1555634;
//        mRectF.top =(float) 2.1555634;
//        mRectF.right = (float) 281.84442;
//        mRectF.bottom = (float) 281.84442;
        //画矩形
         }

    //mRectF.left=2.1555634 mRectF.top=2.1555634 mRectF.right=281.84442 mRectF.bottom=281.84442
    //圆的外接矩形
    private RectF mRectF;
    /**
     * 绘制圆环
     *
     * @param canvas
     */
    /**
     * 绘制圆环
     *
     * @param canvas
     */
    private void drawCircle(Canvas canvas) {
        canvas.save();
        canvas.rotate(270, mCenterPoint.x, mCenterPoint.y);

        //画圆环
        mCirclePaint.setColor(outlineCircleColor);
        canvas.drawArc(mRectF, 0, 360, false, mCirclePaint);

        canvas.restore();
    }
    /**
     * 设置外圆颜色
     */
    public void setOutlineCircleColor(int color){
        outlineCircleColor = color;
    }
    private Path getWavePath() {
//        float changeWaveHeight = (1 - percent) * waveHeight;
        float changeWaveHeight = waveHeight;
        if (onAnimationListener != null) {
            changeWaveHeight =
                    onAnimationListener.howToChangeWaveHeight(percent, waveHeight) == 0 && percent < 1
                            ? waveHeight
                            : onAnimationListener.howToChangeWaveHeight(percent, waveHeight);
        }

        wavePath.reset();

        //移动到右上方，也就是p0点
        wavePath.moveTo(viewSize, (1 - percent) * viewSize);
        //移动到右下方，也就是p1点
        wavePath.lineTo(viewSize, viewSize);
        //移动到左下边，也就是p2点
        wavePath.lineTo(0, viewSize);
        //移动到左上方，也就是p3点
        //wavePath.lineTo(0, (1-percent)*viewSize);
        wavePath.lineTo(-waveMovingDistance, (1 - percent) * viewSize);

        //从p3开始向p0方向绘制波浪曲线
        for (int i = 0; i < waveNum * 2; i++) {
            wavePath.rQuadTo(waveWidth / 2, changeWaveHeight, waveWidth, 0);
            wavePath.rQuadTo(waveWidth / 2, -changeWaveHeight, waveWidth, 0);
        }

        //将path封闭起来
        wavePath.close();
        return wavePath;
    }

    private Path getSecondWavePath() {
//        float changeWaveHeight = (1 - percent) * waveHeight;
        float changeWaveHeight = waveHeight;
        if (onAnimationListener != null) {
            changeWaveHeight =
                    onAnimationListener.howToChangeWaveHeight(percent, waveHeight) == 0 && percent < 1
                            ? waveHeight
                            : onAnimationListener.howToChangeWaveHeight(percent, waveHeight);
        }

        wavePath.reset();

        //移动到左上方，也就是p3点
        wavePath.moveTo(0, (1 - percent) * viewSize);
        //移动到左下边，也就是p2点
        wavePath.lineTo(0, viewSize);
        //移动到右下方，也就是p1点
        wavePath.lineTo(viewSize, viewSize);
        //移动到右上方，也就是p0点
        wavePath.lineTo(viewSize + waveMovingDistance, (1 - percent) * viewSize);

        //从p0开始向p3方向绘制波浪曲线
        for (int i = 0; i < waveNum * 2; i++) {
            wavePath.rQuadTo(-waveWidth / 2, changeWaveHeight, -waveWidth, 0);
            wavePath.rQuadTo(-waveWidth / 2, -changeWaveHeight, -waveWidth, 0);
        }

        //将path封闭起来
        wavePath.close();
        return wavePath;
    }

    public class WaveProgressAnim extends Animation {

        public WaveProgressAnim() {
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (percent < progressNum / maxNum) {
                percent = interpolatedTime * progressNum / maxNum;
                if (textView != null && onAnimationListener != null) {
                    textView.setText(onAnimationListener.howToChangeText(interpolatedTime, progressNum, maxNum));
                }
            }
            waveMovingDistance = interpolatedTime * waveNum * waveWidth * 2;
            postInvalidate();
        }
    }

    /**
     * 设置进度条数值
     *
     * @param progressNum 进度条数值
     * @param time        动画持续时间
     */
    public void setProgressNum(float progressNum, int time) {
        this.progressNum = progressNum;
        percent = 0;
        startAnimation(time);
    }

    public void startAnimation(int time){
        waveProgressAnim.setDuration(time);
        waveProgressAnim.setRepeatCount(Animation.INFINITE);
        waveProgressAnim.setInterpolator(new LinearInterpolator());
        this.startAnimation(waveProgressAnim);
    }

    /**
     * 设置最大进度
     */
    public void setMax(float max) {
        this.maxNum = max;
    }

    /**
     * 是否绘制第二层波浪
     *
     * @param isDrawSecondWave
     */
    public void setDrawSecondWave(boolean isDrawSecondWave) {
        this.isDrawSecondWave = isDrawSecondWave;
    }

    /**
     * 设置显示文字的TextView
     *
     * @param textView
     */
    public void setTextView(TextView textView) {
        this.textView = textView;
    }

    public interface OnAnimationListener {

        /**
         * 如何处理要显示的文字内容
         *
         * @param interpolatedTime 从0渐变成1,到1时结束动画
         * @param updateNum        进度条数值
         * @param maxNum           进度条最大值
         * @return
         */
        String howToChangeText(float interpolatedTime, float updateNum, float maxNum);

        /**
         * 如何处理波浪高度
         *
         * @param percent    进度占比
         * @param waveHeight 波浪高度
         * @return
         */
        float howToChangeWaveHeight(float percent, float waveHeight);
    }

    public void setOnAnimationListener(OnAnimationListener onAnimationListener) {
        this.onAnimationListener = onAnimationListener;
    }

}
